﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FriendlyFireLibrary;
using System.Security.Cryptography;

namespace IndividualClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        FriendlyFireLibrary.NetworkAsyncCodeClient client;

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            client.SendMessage(txtClientMessage.Text, txtClientName.Text);
            txtClientOutput.Text += txtClientName.Text + " : " + txtClientMessage.Text + Environment.NewLine;
            txtClientMessage.Text = "";
        }

        private void MyFunc(byte[] data, int type)
        {
            if (type == (int)Enumerations.DataTypes.String)
            {
                string searchTerms = Encoding.ASCII.GetString(data);
                AddText(searchTerms);
            }
            if (type == (int)Enumerations.DataTypes.DataSet)
            {

            }
        }
        private delegate void AddTextDelegate(string text);
        public void AddText(string text)
        {
            if (txtClientOutput.InvokeRequired)
            {
                txtClientOutput.Invoke(new AddTextDelegate(AddText), text);
            }
            else
            {
                txtClientOutput.Text += text.ToString() + Environment.NewLine;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Control c in this.Controls)
            {
                c.ContextMenuStrip = contextMenuStrip1;
            }
        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (txtClientName.Text == "Client Name")
            {
                txtClientName.Text = "";
                txtClientName.ForeColor = Color.Black;
            }
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (txtClientName.Text == "")
            {
                txtClientName.Text = "Client Name";
                txtClientName.ForeColor = Color.LightGray;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(client != null)
                if(client.connected)
                    client.Shutdown();
        }
        
        private void Connect(object sender, EventArgs e)
        {
            if (btnConnect.Text == "Connect")
            {
                //client = new FriendlyFireLibrary.NetworkAsyncCodeClient(txtIpAddress.Text, pgBar, txtClientName.Text);
                
                client = new NetworkAsyncCodeClient(txtIpAddress.Text, new RSAParameters(), new RSAParameters(), txtClientName.Text);
                client.dataReceivedEvent += new NetworkCodeCommon.DataReceived(client_dataReceivedEvent);
                client.textOutEvent += new NetworkCodeCommon.OnTextOut(client_textOutEvent);
                if (client.hostnameResolved)
                {
                    btnSendMessage.Enabled = true;
                    btnRequest.Enabled = true;
                    btnSetPath.Enabled = true;
                    btnConnect.Text = "Disconnect";
                }
                else
                {
                    txtClientOutput.Text += "Client Failed to Connect to : " + txtIpAddress.Text;
                    client = null;
                }
            }
            else
            {
                if(client.connected)
                    client.Shutdown();
                client = null;
                btnSendMessage.Enabled = false;
                btnRequest.Enabled = false;
                btnSetPath.Enabled = false;
                btnConnect.Text = "Connect";
            }
        }

        void client_textOutEvent(string text)
        {
            AddTextOut(text + Environment.NewLine);
        }

        private delegate void AddTextOutput(string item);
        public void AddTextOut(string item)
        {
            if (this.txtClientOutput.InvokeRequired)
            {
                this.txtClientOutput.Invoke(new AddTextOutput(AddTextOut), item);
            }
            else
            {
                this.txtClientOutput.Text += item;
            }
        }

        void client_dataReceivedEvent(byte[] data, int dataType)
        {
            MessageBox.Show(Encoding.ASCII.GetString(data));
        }

        private void btnRequest_Click(object sender, EventArgs e)
        {
            FileRequester req = new FileRequester();
            req.ShowDialog();
            if (req.SELECTEDFILE != "")
            {
                txtRequest.Text = req.SELECTEDFILE;
                client.RequestFile(txtRequest.Text);
            }
        }

        private void btnSetPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            client.SetDir(folderBrowserDialog1.SelectedPath);
        }

        private void txtClientMessage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter && txtClientMessage.Text != "")
            {
                client.SendMessage(txtClientMessage.Text, txtClientName.Text);
                txtClientOutput.Text += txtClientName.Text + " : " + txtClientMessage.Text + Environment.NewLine;
            }
        }

        private void txtClientOutput_TextChanged(object sender, EventArgs e)
        {
            txtClientOutput.SelectionStart = txtClientOutput.Text.Length;
            txtClientOutput.ScrollToCaret();
        }

        private void txtClientOutput_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void txtClientMessage_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter && txtClientMessage.Text != "")
            {
                txtClientMessage.Text = "";
            }
        }
        bool pause = false;
        private void pauseTransfersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pause = !pause;
            client.Pause(pause);
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip1.Show(new Point(MousePosition.X, MousePosition.Y));
            }
        }

        private void maxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            client.SetMaxRecv(0);
            client.SetMaxSend(0);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            client.SetMaxRecv(10);
            client.SetMaxSend(10);
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            client.SetMaxRecv(50);
            client.SetMaxSend(50);
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            client.SetMaxRecv(100);
            client.SetMaxSend(100);
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            client.SetMaxRecv(500);
            client.SetMaxSend(500);
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            client.SetMaxRecv(1000);
            client.SetMaxSend(1000);
        }
    }
}
